<template>
	<scroll-view scroll-y="true" class="container" :data-theme="themeStyle" v-if="addonIsExit.memberregister">
		<view class="iconfont iconclose back-btn" @click="$util.goBack()"></view>
		<view class="header-wrap">
			<view class="title">企业员工注册</view>
			
		</view>
		<view class="body-wrap" style="margin-top: 0;">
			<view class="form-wrap">
				<view class="input-wrap" v-show="registerMode == 'account'">
					<view class="content"><input type="text" placeholder="请输入用户名" placeholder-class="input-placeholder" class="input"
						 v-model="formData.name" /></view>
				</view>
				<view class="input-wrap" v-show="registerMode == 'account'">
				请选择您所在的企业
					<picker @change="bindPickerChange" :value="index" :range="array">
					            <view class="in2" >{{array[index]}}</view>
					 </picker>
				</view>
				<view class="input-wrap" v-show="registerMode == 'account'">
					<view class="content"><input type="number" placeholder="请输入账号(手机)" maxlength="11" placeholder-class="input-placeholder" class="input"
						 v-model="formData.account" /></view>
				</view>
				<view class="input-wrap">
					<view class="content"><input type="password" placeholder="请输入密码" placeholder-class="input-placeholder" class="input"
						 v-model="formData.password" /></view>
				</view>
				<view class="input-wrap">
					<view class="content">
						<input type="password" placeholder="请确认密码" placeholder-class="input-placeholder" class="input" v-model="formData.rePassword" />
					</view>
				</view>
				<view class="input-wrap">
					<view class="content">
						<input type="text" placeholder="请输入验证码" placeholder-class="input-placeholder" class="input" v-model="formData.vercode" />
						<image :src="captcha.img" mode="" class="captcha" @click="getCaptcha"></image>
					</view>
				</view>
			</view>
			<view class="btn_view">
			<button type="primary" @click="register" class="login-btn ns-border-color ns-bg-color">注册</button>
			</view>
			<view class="regisiter-agreement">
				点击注册即代表您已同意
				<text class="ns-text-color" @click="openPopup">《注册协议》</text>
			</view>
		</view>

		<view @touchmove.prevent>
			<uni-popup ref="registerPopup" type="center" class="wap-floating" :maskClick="false">
				<view class="conten-box">
					<text class="iconfont iconclose" @click="toClose"></text>
					<view class="title">{{ regisiterAgreement.title }}</view>
					<view class="con">
						<scroll-view scroll-y="true" class="con"><rich-text :nodes="regisiterAgreement.content"></rich-text></scroll-view>
					</view>
				</view>
			</uni-popup>
		</view>
		<ns-show-toast></ns-show-toast>
	</scroll-view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import nsShowToast from '@/components/ns-show-toast/ns-show-toast.vue'
	import validate from 'common/js/validate.js';
	import auth from 'common/js/auth.js';
	import globalConfig from '@/common/js/golbalConfig.js'

	export default {
		components: {
			uniPopup,
			nsShowToast
		},
		data() {
			return {
				allowRegister: true, // 是否允许注册
				registerMode: 'account',
				array: ['中国', '美国', '巴西', '日本'],
				index:0,
				formData: {
					mobile: '',
					phone:'',
					gongsi:'',
					account: '',
					password: '',
					rePassword: '',
					vercode: '',
					dynacode: '',
					key: ''
				},
				regisiterAgreement: {
					// 注册协议
					title: '',
					content: ''
				},
				captcha: {
					// 验证码
					id: '',
					img: ''
				},
				isSub: false,
				back: '' // 返回页
			};
		},
		mixins: [auth,globalConfig],
		onLoad(option) {
			if (option.back) this.back = option.back;
			this.getCaptcha();

			if (uni.getStorageSync('authInfo')) {
				let data = uni.getStorageSync('authInfo');
				if (data.authInfo) this.authInfo = data.authInfo;
				if (data.userInfo) this.userInfo = data.userInfo;
			}
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			// if(!this.addonIsExit.memberregister){
			// 	this.$util.showToast({
			// 		title:'商家未开启注册',
			// 		mask:true,
			// 		duration:2000
			// 	})
			// 	setTimeout(()=>{
			// 		this.$util.redirectTo('/pages/index/index/index',{},'redirectTo')
			// 	},2000)
			// 	return;
			// }
			// #ifdef MP
			this.getCode();
			// #endif
			this.getgongsi();
			this.getRegisiterAggrement();
			this.getRegisterConfig();
			this.getUserInfo();
		},
		methods: {
			// https://fangu.xieenguoji.com/api/login/getQyList
			getgongsi() {
				this.$api.sendRequest({
					url: '/api/login/getQyList',
					success: res => {
						this.array=res.list
						this.formData.gongsi=res.list[0]
						// for(var i;i<array2.length;i++){
						// 	this.array.push(this.array2[i])
						// 	console.log(this.array)
						// }
					}
				});
			},
			bindPickerChange: function(e) {
			            console.log('picker发送选择改变，携带值为', e.target.value)
			            this.index = e.target.value
						this.formData.gongsi=this.array[e.target.value]
			        },
			/**
			 * 切换注册方式
			 */
			switchRegisterMode() {
				if (this.registerMode == 'mobile') {
					if (this.registerConfig.type.indexOf('plain') != -1) this.registerMode = 'account';
				} else if (this.registerMode == 'account') {
					if (this.registerConfig.type.indexOf('mobile') != -1) this.registerMode = 'mobile';
				}
			},
			/**
			 * 展示注册协议
			 */
			openPopup() {
				this.$refs.registerPopup.open();
				// if (this.regisiterAgreement.content != '') {
				// 	this.$refs.registerPopup.open();
				// }else{
					
				// }
			},
			/**
			* 点击关闭协议
			*/
			toClose() {
				this.$refs.registerPopup.close();
			},
			/**
			 * 获取注册协议
			 */
			getRegisiterAggrement() {
				this.$api.sendRequest({
					url: '/api/register/aggrement',
					success: res => {
						if (res.code >= 0) {
							this.regisiterAgreement = res.data;
						}
					}
				});
			},
			/**
			 * 获取注册配置
			 */
			getRegisterConfig() {
				this.$api.sendRequest({
					url: '/api/register/config',
					success: res => {
						if (res.code >= 0) {
							this.registerConfig = res.data.value;
							if (this.registerConfig.is_enable != 1) {
								this.$util.showToast({
									title: '平台未启用注册!',
									success: () => {
										setTimeout(() => {
											this.$util.redirectTo('/pages/index/index/index', {}, 'reLaunch');
										}, 1500);
									}
								});
							}
						}
					}
				});
			},
			/**
			 * 获取验证码
			 */
			getCaptcha() {
				this.$api.sendRequest({
					url: '/api/captcha/captcha',
					data: {
						captcha_id: this.captcha.id
					},
					success: res => {
						if (res.code >= 0) {
							this.captcha = res.data;
							this.captcha.img = this.captcha.img.replace(/\r\n/g, '');
						}
					}
				});
			},
			/**
			 * 注册
			 */
			register() {
				var data = {
					username: this.formData.account.trim(),
					password: this.formData.password,
					type:2,
					member_id:this.formData.gongsi
					
				};
				if (this.captcha.id != '') {
					data.captcha_id = this.captcha.id;
					data.captcha_code = this.formData.vercode;
				}
				if (Object.keys(this.authInfo).length) {
					data[this.authInfo.auth_tag] = this.authInfo.auth_openid;
				}
				if (this.userInfo.avatarUrl != '') data.headimg = this.userInfo.avatarUrl;
				if (uni.getStorageSync('source_member')) data.source_member = uni.getStorageSync('source_member');

				if (this.verify(data)) {
					if (this.isSub) return;
					this.isSub = true;
					this.$api.sendRequest({
						url: '/api/register/username',
						data,
						success: res => {
							if (res.code >= 0) {
								uni.setStorage({
									key: 'token',
									data: res.data.token,
									success: () => {
										uni.removeStorageSync('loginLock');
										uni.removeStorageSync('unbound');
										uni.removeStorageSync('authInfo');
										if (this.back != '') {
											this.$util.redirectTo(this.back, {}, 'reLaunch');
										} else {
											this.$util.redirectTo('/pages/member/index/index', {}, 'reLaunch');
										}
									}
								});
							} else {
								this.isSub = false;
								this.getCaptcha();
								this.$util.showToast({
									title: res.message
								});
							}
						},
						fail: res => {
							this.isSub = false;
							this.getCaptcha();
						}
					});
				}
			},
			verify(data) {
				let rule = [{
							name: 'username',
							checkType: 'required',
							errorMsg: '请输入账号'
						},
						{
							name: 'password',
							checkType: 'required',
							errorMsg: '请输入密码'
						}
					],
					regConfig = this.registerConfig;

				if (regConfig.pwd_len > 0) {
					rule.push({
						name: 'password',
						checkType: 'lengthMin',
						checkRule: regConfig.pwd_len,
						errorMsg: '密码长度不能小于' + regConfig.pwd_len + '位'
					});
				}
				if (regConfig.pwd_complexity != '') {
					let passwordErrorMsg = '密码需包含',
						reg = '';
					if (regConfig.pwd_complexity.indexOf('number') != -1) {
						reg += '(?=.*?[0-9])';
						passwordErrorMsg += '数字';
					}
					if (regConfig.pwd_complexity.indexOf('letter') != -1) {
						reg += '(?=.*?[a-z])';
						passwordErrorMsg += '、小写字母';
					}
					if (regConfig.pwd_complexity.indexOf('upper_case') != -1) {
						reg += '(?=.*?[A-Z])';
						passwordErrorMsg += '、大写字母';
					}
					if (regConfig.pwd_complexity.indexOf('symbol') != -1) {
						reg += '(?=.*?[#?!@$%^&*-])';
						passwordErrorMsg += '、特殊字符';
					}
					rule.push({
						name: 'password',
						checkType: 'reg',
						checkRule: reg,
						errorMsg: passwordErrorMsg
					});
				}
				if (this.formData.account.length != 11) {
					this.$util.showToast({
						title: '手机号不正确'
					});
					return false;
				}
				 if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.formData.account))){  
						this.$util.showToast({
							title: '手机号不正确'
						});
				        return false; 
				    } 
				if (this.formData.password != this.formData.rePassword) {
					this.$util.showToast({
						title: '两次密码不一致'
					});
					return false;
				}

				if (this.captcha.id != '') {
					rule.push({
						name: 'captcha_code',
						checkType: 'required',
						errorMsg: '请输入验证码'
					});
				}
				var checkRes = validate.check(data, rule);
				if (checkRes) {
					return true;
				} else {
					this.$util.showToast({
						title: validate.error
					});
					return false;
				}
			},
			/**
			 * 去登录
			 */
			toLogin() {
				if (this.back) this.$util.redirectTo('/pages/login/login/login', {
					back: this.back
				});
				else this.$util.redirectTo('/pages/login/login/login');
			}
		}
	};
</script>

<style lang="scss">
	@import '../public/css/common.scss';
</style>
